package edu.ncsu.csc.camera;

import java.lang.StringBuffer;

public class ByteVector
{
	private byte[] data;
	int size;
	int elements;

	ByteVector(byte[] b)
	{
		data = new byte[b.length];
		size = b.length;
		elements = size;
		for(int k = 0; k < elements; k++)
			data[k] = b[k];
	}

	ByteVector(int initial_size)
	{
		size = initial_size;
		data = new byte[size];
		elements = 0;
	}
	
	void add(byte b)
	{
		if(elements < size)
			data[elements] = b;
		else
		{
			size *= 2;
			byte[] new_data = new byte[size];
			for(int k = 0; k < elements; k++)
				new_data[k] = data[k];
			new_data[elements] = b;
			data = new_data;
		}
		elements++;
	}
	
	byte get(int i)
	{
		return data[i];
	}
	
	void add(char c)
	{
		add((byte)c);
	}
	
	int length()
	{
		return elements;
	}

	/* only use this method for print ASCII!
		I'm including an extra '\n' every time I get a '\r'
		for the literal response use writeByteArray()
	*/
	
	public String toString()
	{
		StringBuffer buffer = new StringBuffer(elements);
		for(int k = 0; k < elements; k++)
		{
			buffer.append((char)data[k]);
			if(data[k] == '\r')
				buffer.append('\n');
		}
		return buffer.toString();
	}
}